<?php

require_once 'config.php';

$responseData = (array)simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);

$pdo = new \PDO('mysql:host=' . $mysql_config['hostname'] . ';dbname=' . $mysql_config['database'], $mysql_config['username'], $mysql_config['password']);
$pdo->exec('set names utf8');

//微信付款回调

if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {
	
	$out_trade_no = $responseData['out_trade_no']; //商户订单号
	$total_fee = $responseData['total_fee']; //订单金额/分
	$trade_no = $responseData['transaction_id']; //微信订单号
	
	$total_amount = $total_fee / 100;
	
	$type = substr($out_trade_no, 0, 1);
	
	if ($type == $type_config['type_1']) {
		//商品
	} else if ($type == $type_config['type_2']) {
		//项目

		// 修改订单状态
		// 修改用户金额
		// 修改项目销售数量
		// 修改店铺余额
		// 写入店铺资金流水
		// 写入预约记录如果购买的时候选择了预约时间

		//订单信息
		$order_query_sql = 'SELECT id, store_id, ctm_id, staff_id, starttime FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
		$order_query_stat = $pdo->prepare($order_query_sql);
		$order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
		if ($order_query_result) {
			$order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
			if ($order_query_row) {
				$order_id = $order_query_row['id'];
				$store_id = $order_query_row['store_id'];
				$ctm_id = $order_query_row['ctm_id'];
				$staff_id = $order_query_row['staff_id'];
				$starttime = $order_query_row['starttime'];

				//订单产品信息
				$ord_pro_query_sql = 'SELECT id, pro_id, number FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
				$ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
				$ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
				$ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
				$pro_id = $ord_pro_query_row['pro_id'];
				$number = $ord_pro_query_row['number'];

				//用户信息
				$custom_query_sql = 'SELECT id, custom_phone, custom_name, consume_money FROM da_custom WHERE id = :id';
				$custom_query_stat = $pdo->prepare($custom_query_sql);
				$custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
				$custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
				$custom_phone = $custom_query_row['custom_phone'];
				$custom_name = $custom_query_row['custom_name'];
				$consume_money = $custom_query_row['consume_money'];

				//店铺信息
				$store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
				$store_query_stat = $pdo->prepare($store_query_sql);
				$store_query_result = $store_query_stat->execute([':id' => $store_id]);
				$store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
				$store_money = $store_query_row['money'];


				$pdo->beginTransaction();
				
				$time = time();
				$flow_number = $custom_phone . $time . mt_rand(1000, 9999);

				//写入预约记录
				if ($staff_id == 0) {
					$ctm_staff_insert_result = true;
				} else {
					$ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
					$ctm_staff_insert_sql = "INSERT INTO da_ctm_staff (staff_id, ctm_id, item_id, store_id, order_id, number, ctm_name, ctm_phone, status, add_time, start_time) VALUES ({$staff_id}, {$ctm_id}, {$pro_id}, {$store_id}, {$order_id}, 1, '{$ctm_name}', '{$custom_phone}', 3, {$time}, {$starttime})";
					$ctm_staff_insert_result = $pdo->exec($ctm_staff_insert_sql);
				}
				
				//修改订单状态
				$status = $status_config['status_2'];
				if ($staff_id != 0) {
					if ($number == 1) {
						$status = 3;
					}
				}
				$order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_4']}, status = {$status}, pay_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
				$order_status_result = $pdo->exec($order_status_sql);

				//修改用户金额
				$custom_money_sql = "UPDATE da_custom SET consume_money = consume_money + {$total_amount} WHERE id = {$ctm_id}";
				$custom_money_result = $pdo->exec($custom_money_sql);

				//修改项目销售数量
				$item_update_sql = "UPDATE da_item SET sell_count = sell_count + 1 WHERE id = {$pro_id}";
				$item_update_result = $pdo->exec($item_update_sql);

				//修改店铺余额
				$store_money_sql = "UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}";
				$store_money_result = $pdo->exec($store_money_sql);

				//写入店铺资金流水
				$flow_last_money = $store_money + $total_amount;
				$flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_2']}, {$moneyflow_config['pay_method_4']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
				$flow_insert_result = $pdo->exec($flow_insert_sql);
				
				if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $item_update_result && $store_money_result && $flow_insert_result) {
					$pdo->commit();
				} else {
					$pdo->rollBack();
				}

			}
		}
		
		
	} else if ($type == $type_config['type_3']) {
		//VIP卡

		// 修改订单状态
		// 修改用户金额
		// 写入vip卡资金流水
		// 修改对应vip卡销售数量和销售金额
		// 修改店铺余额
		// 写入店铺资金流水

		//订单信息
		$order_query_sql = 'SELECT id, store_id, ctm_id FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
		$order_query_stat = $pdo->prepare($order_query_sql);
		$order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
		if ($order_query_result) {
			$order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
			if ($order_query_row) {
				$order_id = $order_query_row['id'];
				$store_id = $order_query_row['store_id'];
				$ctm_id = $order_query_row['ctm_id'];

				//订单产品信息
				$ord_pro_query_sql = 'SELECT id, pro_id FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
				$ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
				$ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
				$ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
				$pro_id = $ord_pro_query_row['pro_id'];

				//用户信息
				$custom_query_sql = 'SELECT id, custom_phone, charge_money, real_money, consume_money FROM da_custom WHERE id = :id';
				$custom_query_stat = $pdo->prepare($custom_query_sql);
				$custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
				$custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
				$custom_phone = $custom_query_row['custom_phone'];
				$charge_money = $custom_query_row['charge_money'];
				$real_money = $custom_query_row['real_money'];
				$consume_money = $custom_query_row['consume_money'];

				//店铺信息
				$store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
				$store_query_stat = $pdo->prepare($store_query_sql);
				$store_query_result = $store_query_stat->execute([':id' => $store_id]);
				$store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
				$store_money = $store_query_row['money'];

				//vip卡信息
				$vipcard_query_sql = 'SELECT id, vip_lines, vip_discount FROM da_vipcard WHERE id = :id';
				$vipcard_query_stat = $pdo->prepare($vipcard_query_sql);
				$vipcard_query_result = $vipcard_query_stat->execute([':id' => $pro_id]);
				$vipcard_query_row = $vipcard_query_stat->fetch(\PDO::FETCH_ASSOC);
				$vipcard_id = $vipcard_query_row['id'];
				$vip_lines = $vipcard_query_row['vip_lines'];
				$vip_discount = $vipcard_query_row['vip_discount'];
				
				
				$pdo->beginTransaction();
				
				$time = time();
				$flow_number = $custom_phone . $time . mt_rand(1000, 9999);

				//修改订单状态
				$order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_4']}, status = {$status_config['STATUS_11']}, pay_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
				$order_status_result = $pdo->exec($order_status_sql);

				//修改用户金额
				$vip_real_money = floor($vip_lines / ($vip_discount / 10) * 100) / 100;
				$custom_money_sql = "UPDATE da_custom SET vip_status = 2, charge_money = charge_money + {$vip_lines}, real_money = real_money + {$vip_real_money}, consume_money = consume_money + {$vip_lines} WHERE id = {$ctm_id}";
				$custom_money_result = $pdo->exec($custom_money_sql);

				//写入vip卡资金流水
				$last_money = $real_money + $vip_real_money;
				$ctmflow_insert_sql = "INSERT INTO da_ctmmoneyflow (type, pay_method, store_id, ctm_id, item_id, money, last_money, flow_number, add_time) VALUES (1, {$ordtype_config['ordtype_4']}, {$store_id}, {$ctm_id}, {$pro_id}, {$vip_real_money}, {$last_money}, '{$flow_number}', {$time})";
				$ctmflow_insert_result = $pdo->exec($ctmflow_insert_sql);

				//修改对应vip卡销售数量和销售金额
				$vipcard_update_sql = "UPDATE da_vipcard SET sell_count = sell_count + 1, sell_money = sell_money + {$vip_lines} WHERE id = {$pro_id}";
				$vipcard_update_result = $pdo->exec($vipcard_update_sql);

				//修改店铺余额
				$store_money_sql = "UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}";
				$store_money_result = $pdo->exec($store_money_sql);

				//写入店铺资金流水
				$flow_last_money = $store_money + $total_amount;
				$flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_3']}, {$moneyflow_config['pay_method_4']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
				$flow_insert_result = $pdo->exec($flow_insert_sql);

				if ($order_status_result && $custom_money_result && $ctmflow_insert_result && $vipcard_update_result && $store_money_result && $flow_insert_result) {
					$pdo->commit();

				} else {
					$pdo->rollBack();
				}
			}
		}
		

	} else if ($type == $type_config['type_4']) {
		//体验卡

		// 修改订单状态
		// 修改用户金额
		// 写入体验卡购买记录
		// 修改店铺余额
		// 写入店铺资金流水

		//订单信息
		$order_query_sql = 'SELECT id, store_id, ctm_id FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
		$order_query_stat = $pdo->prepare($order_query_sql);
		$order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
		if ($order_query_result) {
			$order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
			if ($order_query_row) {
				$order_id = $order_query_row['id'];
				$store_id = $order_query_row['store_id'];
				$ctm_id = $order_query_row['ctm_id'];

				//订单产品信息
				$ord_pro_query_sql = 'SELECT id, pro_id FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
				$ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
				$ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
				$ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
				$pro_id = $ord_pro_query_row['pro_id'];

				//用户信息
				$custom_query_sql = 'SELECT id, custom_phone, consume_money FROM da_custom WHERE id = :id';
				$custom_query_stat = $pdo->prepare($custom_query_sql);
				$custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
				$custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
				$custom_phone = $custom_query_row['custom_phone'];
				$consume_money = $custom_query_row['consume_money'];

				//店铺信息
				$store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
				$store_query_stat = $pdo->prepare($store_query_sql);
				$store_query_result = $store_query_stat->execute([':id' => $store_id]);
				$store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
				$store_money = $store_query_row['money'];


				$pdo->beginTransaction();
				
				$time = time();
				$flow_number = $custom_phone . $time . mt_rand(1000, 9999);

				//修改订单状态
				$order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_4']}, status = {$status_config['status_2']}, pay_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
				$order_status_result = $pdo->exec($order_status_sql);

				//修改用户金额
				$custom_money_sql = "UPDATE da_custom SET consume_money = consume_money + {$total_amount} WHERE id = {$ctm_id}";
				$custom_money_result = $pdo->exec($custom_money_sql);

				//写入体验卡购买记录
				$start_time = time();
				$end_time = $start_time + 31 * 24 * 3600;
				$expercard_custom_insert_sql = "INSERT INTO da_expercard_custom (store_id, expercard_id, custom_id, price, start_time, end_time, add_time) VALUES ({$store_id}, {$pro_id}, {$ctm_id}, {$total_amount}, {$start_time}, {$end_time}, {$start_time})";
				$expercard_custom_insert_result = $pdo->exec($expercard_custom_insert_sql);

				//修改店铺余额
				$store_money_sql = "UPDATE da_store SET money = money + {$total_amount}  WHERE id = {$store_id}";
				$store_money_result = $pdo->exec($store_money_sql);

				//写入店铺资金流水
				$flow_last_money = $store_money + $total_amount;
				$flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_4']}, {$moneyflow_config['pay_method_4']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
				$flow_insert_result = $pdo->exec($flow_insert_sql);

				if ($order_status_result && $custom_money_result && $expercard_custom_insert_result && $store_money_result && $flow_insert_result) {
					$pdo->commit();
				} else {
					$pdo->rollBack();
				}

			}
		}
		

	} else if ($type == $type_config['type_5']) {
		//限时抢购

		// 修改订单状态
		// 修改用户金额
		// 限时抢购减少库存
		// 写入限时抢购购买记录
		// 修改店铺余额
		// 写入店铺资金流水
		// 写入预约记录如果购买的时候选择了预约时间

		//订单信息
		$order_query_sql = 'SELECT id, store_id, ctm_id, staff_id, starttime FROM da_order WHERE ord_num = :ord_num ORDER BY add_time DESC';
		$order_query_stat = $pdo->prepare($order_query_sql);
		$order_query_result = $order_query_stat->execute([':ord_num' => $out_trade_no]);
		if ($order_query_result) {
			$order_query_row = $order_query_stat->fetch(\PDO::FETCH_ASSOC);
			if ($order_query_row) {
				$order_id = $order_query_row['id'];
				$store_id = $order_query_row['store_id'];
				$ctm_id = $order_query_row['ctm_id'];
				$staff_id = $order_query_row['staff_id'];
				$starttime = $order_query_row['starttime'];

				//订单产品信息
				$ord_pro_query_sql = 'SELECT id, pro_id, number FROM da_ord_pro WHERE ord_id = :ord_id ORDER BY add_time DESC';
				$ord_pro_query_stat = $pdo->prepare($ord_pro_query_sql);
				$ord_pro_query_result = $ord_pro_query_stat->execute([':ord_id' => $order_id]);
				$ord_pro_query_row = $ord_pro_query_stat->fetch(\PDO::FETCH_ASSOC);
				$pro_id = $ord_pro_query_row['pro_id'];
				$number = $ord_pro_query_row['number'];
				
				//限时抢购信息
				$miaosha_query_sql = 'SELECT id, item_id FROM da_miaosha WHERE id = :id';
				$miaosha_query_stat = $pdo->prepare($miaosha_query_sql);
				$miaosha_query_result = $miaosha_query_stat->execute([':id' => $pro_id]);
				$miaosha_query_row = $miaosha_query_stat->fetch(\PDO::FETCH_ASSOC);
				$item_id = $miaosha_query_row['item_id'];
				
				//用户信息
				$custom_query_sql = 'SELECT id, custom_phone, custom_name, consume_money FROM da_custom WHERE id = :id';
				$custom_query_stat = $pdo->prepare($custom_query_sql);
				$custom_query_result = $custom_query_stat->execute([':id' => $ctm_id]);
				$custom_query_row = $custom_query_stat->fetch(\PDO::FETCH_ASSOC);
				$custom_phone = $custom_query_row['custom_phone'];
				$custom_name = $custom_query_row['custom_name'];
				$consume_money = $custom_query_row['consume_money'];

				//店铺信息
				$store_query_sql = 'SELECT id, money FROM da_store WHERE id = :id';
				$store_query_stat = $pdo->prepare($store_query_sql);
				$store_query_result = $store_query_stat->execute([':id' => $store_id]);
				$store_query_row = $store_query_stat->fetch(\PDO::FETCH_ASSOC);
				$store_money = $store_query_row['money'];


				$pdo->beginTransaction();
				
				$time = time();
				$flow_number = $custom_phone . $time . mt_rand(1000, 9999);

				//写入预约记录
				if ($staff_id == 0) {
					$ctm_staff_insert_result = true;
				} else {
					$ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
					$ctm_staff_insert_sql = "INSERT INTO da_ctm_staff (staff_id, ctm_id, item_id, store_id, order_id, number, ctm_name, ctm_phone, status, add_time, start_time) VALUES ({$staff_id}, {$ctm_id}, {$item_id}, {$store_id}, {$order_id}, 1, '{$ctm_name}', '{$custom_phone}', 3, {$time}, {$starttime})";
					$ctm_staff_insert_result = $pdo->exec($ctm_staff_insert_sql);
				}
				
				//修改订单状态
				$status = $status_config['status_2'];
				if ($staff_id != 0) {
					if ($number == 1) {
						$status = 3;
					}
				}
				$order_status_sql = "UPDATE da_order SET ordtype = {$ordtype_config['ordtype_4']}, status = {$status}, pay_time = {$time}, wx_trade_no = '{$trade_no}' WHERE id = {$order_id}";
				$order_status_result = $pdo->exec($order_status_sql);

				//修改用户金额
				$custom_money_sql = "UPDATE da_custom SET consume_money = consume_money + {$total_amount} WHERE id = {$ctm_id}";
				$custom_money_result = $pdo->exec($custom_money_sql);

				//限时抢购减少库存
				$miaosha_update_sql = "UPDATE da_miaosha SET number = number - 1, sell_count = sell_count + 1 WHERE id = {$pro_id}";
				$miaosha_update_result = $pdo->exec($miaosha_update_sql);

				//写入限时抢购购买记录
				$miaosha_custom_insert_sql = "INSERT INTO da_miaosha_custom (store_id, miaosha_id, custom_id, price, add_time) VALUES ({$store_id}, {$pro_id}, {$ctm_id}, {$total_amount}, {$time})";
				$miaosha_custom_insert_result = $pdo->exec($miaosha_custom_insert_sql);

				//修改店铺余额
				$store_money_sql = "UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}";
				$store_money_result = $pdo->exec($store_money_sql);

				//写入店铺资金流水
				$flow_last_money = $store_money + $total_amount;
				$flow_insert_sql = "INSERT INTO da_moneyflow (type, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$moneyflow_config['type_1']}, {$moneyflow_config['pro_type_5']}, {$moneyflow_config['pay_method_4']}, {$store_id}, 0, {$ctm_id}, {$total_amount}, {$flow_last_money}, '{$flow_number}', {$time})";
				$flow_insert_result = $pdo->exec($flow_insert_sql);
				
				if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $miaosha_update_result && $miaosha_custom_insert_result && $store_money_result && $flow_insert_result) {
					$pdo->commit();
				} else {
					$pdo->rollBack();
				}

			}
		}

	}
	
	
	echo 'success';
} else {
	echo 'fail';
}


?>